// pages/Index/openService/openService.js
const controllers = require('../../controllers/controllerIndex').controller
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    endTime: '',
    payData: {},
    no: '',
    title: "",
    canPay: true,

    videoData:{},
    num:1,
    selectSingle:true,
    selectComplex:false,
  },
  //选择方式
  choice(e) {
    this.setData({
      choiceactive: e.currentTarget.dataset.index,
      way: e.currentTarget.id
    })
  },

  //提交续费订单
  ok() {
console.log(123);

    if (this.data.canPay == false) {
      return
    }
    this.setData({
      canPay: false
    })
    if(this.data.no == 11){
      this.renewShopVideoOrder()
    }else{
      this.configOrder()
    }
  },
  // 视频权限开通
  renewShopVideoOrder(){
    let renewShopVideoId = null
    let num = this.data.num
    const selectSingle = this.data.selectSingle
    const videoData = this.data.videoData
    const currentIndex = this.data.currentIndex
    if(selectSingle){
      renewShopVideoId = videoData.renew.id
    }else{
      num = 1
      renewShopVideoId = videoData.renewList[currentIndex].id
    }
    controllers.renewShopVideoOrder({
      num,
      renewShopVideoId,
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      this.weChatPay(res)
    })
  },
  configOrder(){
    controllers.configOrder({
      no: this.data.no,
      shopId: wx.getStorageSync('shopId')
    }).then(res => {
      this.weChatPay(res)
    })
  },
  weChatPay(res){
    let that = this
    let orderNo = res.data.orderNo
    let accountId = res.data.accountId
    let orderId = res.data.id
    controllers.weChatPay({
      shopId: wx.getStorageSync('shopId'),
      orderNo: orderNo,
      frontUrl: "mp:///pages/Index/Index",
      type: "SP",
      openId: wx.getStorageSync('openId')
    }).then(res => {
      console.log(res);
      // wx.navigateTo({
      //   url: '/pages/weixinlink/weixinlink?url='+encodeURIComponent(res.data)
      // })
      let data = JSON.parse(res.data);
      console.log(data)
      wx.requestPayment({
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.paySign,
        timeStamp: data.timeStamp,
        signType: data.signType,
        success(res) {
          wx.navigateBack()
        },
        fail(res) {
          console.log(res)
        },
        complete(res) {
          that.setData({
            canPay: true
          })
        }
      })
    })
  },
    //获取服务开通费用
  getConfig() {
    controllers.getConfig({
      no: this.data.no
    }).then(res => {
      this.setData({
        payData: res.data,
      })
    })
  },
  // 获取店铺视频费用列表
  renewShopVideoList() {
    controllers.renewShopVideoList({
      no: this.data.no
    }).then(res => {
      this.setData({
        videoData: res.data,
      })
    })
  },
  decrement(){
    let num = this.data.num
    if(num <= 1)return
    num -=1
    this.setData({
      num
    })
  },
  increment(){
    let num = this.data.num
    num +=1
    this.setData({
      num
    })
  },

  chooseServer(e){
    const index = e.currentTarget.dataset.index
    if(typeof index == 'number'){
      this.setData({
        currentIndex: index,
        selectComplex: true,
        selectSingle: false,
      })
    }else{
      this.setData({
        currentIndex: null,
        selectComplex: false,
        selectSingle: true,
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: wx.getStorageSync('shopId'),
      endTime: wx.getStorageSync('endTime').slice(0, 10),
      no: options.no
    })
    if (options.no == 11) {
      this.setData({
        title: "视频上传权限开通"
      })
      this.renewShopVideoList()
    } else if (options.no == 13) {
      this.setData({
        title: "店铺独立品牌小程序"
      })
      this.getConfig()

    } else {
      this.setData({
        title: "店铺入驻抖音版"
      })
      this.getConfig()

    }

  },


})